package scau.edu.cn.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import scau.edu.cn.common.utils.ApiResponse;
import scau.edu.cn.common.utils.FileUtils;
import scau.edu.cn.entity.QueryEntity;
import scau.edu.cn.entity.vo.PhotoVO;
import scau.edu.cn.entity.vo.TypePhotoVO;
import scau.edu.cn.repository.QueryRepository;
import scau.edu.cn.service.QueryService;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @description:
 * @author: yuxiang
 * @create: 2020-08-15 14:34
 **/
@Service
public class QueryServiceImpl implements QueryService {

    /**
     * 每个用户能收到的照片数量值
     */
    private final int ALL_TIMES = 60;

    @Autowired
    private QueryRepository queryRepository;


    @Value("${web.upload-path}")
    private String localPath;

    @Value("${config.type-path}")
    private String configTypePath;

    @Override
    public ApiResponse createQuery(int flag) {
        ApiResponse response = new ApiResponse();
        try {

            /*
               获取图片
            */
            //文件分类
            List<String> list = Arrays.asList(new File(localPath).list());

            List<TypePhotoVO> typePhotoVOS = new ArrayList<>();//分类图片vos
            //获取街巷10张
            List<String> listPhotos1 = Arrays.asList(new File(localPath + "street").list());

            Random rand1 = new Random();
            for (int i = 0; i < 10; i++) {
                int rInt = rand1.nextInt(listPhotos1.size());//此随机数生成器的序列中取出的、在 0（包括）和指定值n（不包括）之间均匀分布的 int值。
                typePhotoVOS.add(new TypePhotoVO("street", "街巷", listPhotos1.get(rInt)));
            }

            //遍历全部分类
            for (String name :
                    list) {

                if (!name.equals("street")) {
//                    System.out.println("分类:"+name);
                    //获取分类中文名称
                    String typeName = FileUtils.getTypeName(configTypePath, name);
                    List<String> listPhotos = Arrays.asList(new File(localPath + name).list());

                    //随机获取5张
                    Random rand = new Random();
                    for (int i = 0; i < ALL_TIMES; i++) {
                        int rInt = rand.nextInt(listPhotos.size());//此随机数生成器的序列中取出的、在 0（包括）和指定值n（不包括）之间均匀分布的 int值。
                        typePhotoVOS.add(new TypePhotoVO(name, typeName, listPhotos.get(rInt)));
                    }
                }
            }


//            PhotoVO vo = new PhotoVO(,typeName,photos.size(),photos);
            response.setObj(typePhotoVOS);
            response.setMaxCount(typePhotoVOS.size());
            response.setMsg("新建问卷成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(-1, "服务器错误");
        }
        return response;
    }

    @Override
    public ApiResponse checkQuery(String id) {
        ApiResponse response = new ApiResponse();
        try {
            if (StringUtils.isEmpty(id) && StringUtils.isBlank(id)) {
                return new ApiResponse(-1, "id为空");
            }
            QueryEntity entity = queryRepository.findQueryEntityById(id);
            if (entity.getFlag() == 1) {
                return new ApiResponse(-1, "问卷已被填写");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(-1, "数据库错误");
        }
        response.setEvent(0);
        response.setMsg("问卷可用");
        return response;
    }

    @Override
    public ApiResponse createQueryByType(String type) {
        ApiResponse response = new ApiResponse();
        try {
            /*
               获取图片
            */
            //文件分类
            List<String> list = Arrays.asList(new File(localPath).list());
            List<TypePhotoVO> typePhotoVOS = new ArrayList<>();//分类图片vos
            List<String> listPhotos = new ArrayList<>();//图片集合
            System.out.println("type===>>"+type);
            if (type.equals("ALL")) {
                //抽取全部
                //全部图片
                List<String> allPhotos = Arrays.asList(new File(localPath + type).list());
                if (allPhotos.size() < ALL_TIMES) {
                    //不够100张,返回全部
                    listPhotos = allPhotos;
                } else {
                    //随机拿取100张
                    Random rand = new Random();
                    for (int i = 0; i < ALL_TIMES; i++) {
                        int rInt = rand.nextInt(allPhotos.size());//此随机数生成器的序列中取出的、在 0（包括）和指定值n（不包括）之间均匀分布的 int值。
                        listPhotos.add(i, allPhotos.get(rInt));
                    }
                }
                System.out.println("走这里===>>"+listPhotos.size());
            } else {
                //获取文件的全部图片
                listPhotos = Arrays.asList(new File(localPath + type).list());
            }

            //获取分类中文名称
            String typeName = FileUtils.getTypeName(configTypePath, type);

            for (int i = 0; i < listPhotos.size(); i++) {
                typePhotoVOS.add(new TypePhotoVO(type, typeName, listPhotos.get(i)));
            }

            response.setObj(typePhotoVOS);
            response.setMaxCount(typePhotoVOS.size());
            response.setMsg("新建问卷成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(-1, "服务器错误");
        }
        return response;
    }
}
